package autohome.wz.lib;

import autohome.wz.dbutil.DbManager;
import autohome.wz.model.AutoNewsComment.NewsCommentMq;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;


/**
 * AutoHomeNewsCommentHelper
 */
public class AutoHomeNewsCommentHelper {
    private static Logger logger = Logger.getLogger(AutoHomeNewsCommentHelper.class);

    /**
     * 获取未处理的抓取评论队列列表
     *
     * @return
     */
    public static List<NewsCommentMq> getNewsCommentMq() {
        List<NewsCommentMq> listCommentMq = null;
        Connection conn = DbManager.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String strSql = "SELECT TOP 100 [id],[news_id],[biz_type],[biz_id],[state],[createtime] FROM [news_comment_mq] WHERE state=0";
        try {
            listCommentMq = queryRunner.query(conn, strSql, new BeanListHandler<NewsCommentMq>(NewsCommentMq.class));
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("获取抓取评论队列列表异常，异常信息：" + e.toString());
        }
        return listCommentMq;
    }

    /**
     * 设置抓取评论队列状态
     *
     * @param id    队列ID
     * @param state 处理状态
     */
    public static void setStateById(int id, int state) {
        Connection conn = DbManager.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String strSql = "UPDATE [news_comment_mq] SET [state]=? WHERE id=?";
        try {
            queryRunner.update(conn, strSql, state, id);

        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("设置抓取评论队列状态异常，异常信息：" + e.toString());
        }
    }

}
